﻿using System;
using UnityEngine;
using Yoozoo.UI.YGUI;

[RequireComponent(typeof(YText))]

public class YTextColorStyleSwitcher : MonoBehaviour
{
    public const string DefaultLabel = "original";
    
    [SerializeField] private MyData[] styles;

    private void Awake()
    {
        var tCom = GetComponent<YText>();
        if (tCom.colorStyle == null)
            return;

        var temp = new MyData[this.styles.Length + 1];
        Array.Copy(this.styles, temp, this.styles.Length);
        temp[this.styles.Length] = new MyData { label = DefaultLabel, color = tCom.colorStyle };
        this.styles = temp;
    }

    public void SetStyle(string label)
    {
        if (string.IsNullOrEmpty(label))
            label = DefaultLabel;

        for (var i = 0; i < this.styles.Length; i++)
        {
            if (label == this.styles[i].label)
            {
                var tCom = GetComponent<YText>();
                if (tCom != null)
                    tCom.colorStyle = this.styles[i].color;

                break;
            }
        }
    }

    [Serializable]
    struct MyData
    {
        public string label;
        public YTextColorStyle color;
    };  
}
